package server

import (
	"context"
	"errors"
	"srv/internal/dao"
	"srv/pkg"
	pb "srv/proto"
)

type Server struct {
	pb.UnimplementedUserServer
}

// 医生注册
func (s *Server) DoctorRegister(c context.Context, req *pb.DoctorRegisterReq) (*pb.DoctorRegisterResp, error) {
	d := dao.Doctor{
		Name:          req.Name,
		Image:         req.Image,
		Hospital:      req.Hospital,
		Room:          req.Intro,
		NickName:      req.Name,
		Type:          int8(req.Type),
		RegisterPrice: float64(req.RegisterPrice),
		InspectPrice:  float64(req.InspectPrice),
		Intro:         req.Intro,
	}
	id, message := d.DoctorRegister()
	if message != "" {
		return &pb.DoctorRegisterResp{Id: int64(id.ID)}, errors.New(message)
	}
	//同步es
	pkg.SyncEs(id)
	return &pb.DoctorRegisterResp{Id: int64(id.ID)}, nil
}

// 医生展示
func (s *Server) DoctorList(c context.Context, req *pb.DoctorListReq) (*pb.DoctorListResp, error) {
	/*list := pkg.ESList()*/
	//es查不到数据就去差mysql
	/*if list == nil {
		var d dao.Doctor
		var List []*pb.DoctorList
		l, message := d.DoctorList()
		if message != "" {
			return &pb.DoctorListResp{List: nil}, errors.New(message)
		}
		for _, v := range l {
			ll := pb.DoctorList{
				Name:          v.Name,
				Image:         v.Image,
				Hospital:      v.Hospital,
				Root:          v.Intro,
				Nickname:      v.Room,
				Type:          int64(v.Type),
				RegisterPrice: float32(v.RegisterPrice),
				InspectPrice:  float32(v.InspectPrice),
				Intro:         v.Intro,
				Id:            int64(v.ID),
			}
			List = append(List, &ll)
		}
		return &pb.DoctorListResp{List: List}, nil
	} else {
		return &pb.DoctorListResp{List: list}, nil
	}*/
	var d dao.Doctor
	var List []*pb.DoctorList
	l, message := d.DoctorList()
	if message != "" {
		return &pb.DoctorListResp{List: nil}, errors.New(message)
	}
	for _, v := range l {
		ll := pb.DoctorList{
			Name:          v.Name,
			Image:         v.Image,
			Hospital:      v.Hospital,
			Root:          v.Intro,
			Nickname:      v.Room,
			Type:          int64(v.Type),
			RegisterPrice: float32(v.RegisterPrice),
			InspectPrice:  float32(v.InspectPrice),
			Intro:         v.Intro,
			Id:            int64(v.ID),
		}
		List = append(List, &ll)
	}
	return &pb.DoctorListResp{List: List}, nil
}

// 预约
func (s *Server) MessageCreate(c context.Context, req *pb.MessageCreateReq) (*pb.MessageCreateResp, error) {
	m := dao.Message{
		Name:            req.Name,
		IdCardType:      int8(req.IdCardType),
		WXName:          req.WXName,
		Address:         req.Address,
		MedicalIdCard:   req.MedicalIdCard,
		IdCard:          req.IdCard,
		Mobile:          req.Mobile,
		Image:           req.Image,
		Relationship:    int8(req.Relationship),
		Hospital:        req.Hospital,
		HospitalAddress: req.HospitalAddress,
		Room:            req.Room,
		DoctorName:      req.DoctorName,
		NickName:        req.Nickname,
		Type:            int8(req.Type),
		Time:            req.Time,
		WaitTime:        req.WaitTime,
		RegisterPrice:   float64(req.RegisterPrice),
		CreateTime:      req.CreateTime,
		Status:          int8(req.Status),
		Cancel:          req.Cancel,
		CancelTime:      req.CancelTime,
		Comment:         req.Comment,
	}
	id, message := m.MessageCreate()
	if message != "" {
		return &pb.MessageCreateResp{Id: int64(id)}, errors.New(message)
	}
	return &pb.MessageCreateResp{Id: int64(id)}, nil
}

// 查看预约信息
func (s *Server) MessageShow(c context.Context, req *pb.MessageShowReq) (*pb.MessageShowResp, error) {
	var m dao.Message
	mm, message := m.MessageShow(req.Id)
	if message != "" {
		return &pb.MessageShowResp{
			Name:            "",
			IdCardType:      0,
			WXName:          "",
			Address:         "",
			MedicalIdCard:   "",
			IdCard:          "",
			Image:           "",
			Mobile:          "",
			Relationship:    0,
			Hospital:        "",
			HospitalAddress: "",
			Room:            "",
			DoctorName:      "",
			Nickname:        "",
			Type:            0,
			Time:            "",
			WaitTime:        "",
			RegisterPrice:   0,
			CreateTime:      "",
			Status:          0,
			Cancel:          "",
			CancelTime:      "",
			Comment:         "",
		}, errors.New(message)
	}
	return &pb.MessageShowResp{
		Name:            mm.Name,
		IdCardType:      int64(mm.IdCardType),
		WXName:          mm.WXName,
		Address:         mm.Address,
		MedicalIdCard:   mm.MedicalIdCard,
		IdCard:          mm.IdCard,
		Image:           mm.Image,
		Mobile:          mm.Mobile,
		Relationship:    int64(mm.Relationship),
		Hospital:        mm.Hospital,
		HospitalAddress: mm.HospitalAddress,
		Room:            mm.Room,
		DoctorName:      mm.DoctorName,
		Nickname:        mm.NickName,
		Type:            int64(mm.Type),
		Time:            mm.Time,
		WaitTime:        mm.WaitTime,
		RegisterPrice:   float32(mm.RegisterPrice),
		CreateTime:      mm.CreateTime,
		Status:          int64(mm.Status),
		Cancel:          mm.Cancel,
		CancelTime:      mm.CancelTime,
		Comment:         mm.Comment,
	}, nil
}

// 修改预约信息
func (s *Server) MessageUpdate(c context.Context, req *pb.MessageUpdateReq) (*pb.MessageUpdateResp, error) {
	var m dao.Message
	message := m.MessageUpdate(req.Cancel, req.Id)
	if message != "" {
		return &pb.MessageUpdateResp{}, errors.New(message)
	}
	return &pb.MessageUpdateResp{}, nil
}
